﻿var isedit=false;
var row;
$(function() {
	$('#SelectGroupList').combobox({
		valueField: 'GroupID',
		textField: 'GroupName'
	});


	$('#btn-search,#btn-search-cancel').linkbutton();
	searchWin = $('#search-window').window({
		closed: true,
		modal: true
	});
	searchForm = searchWin.find('form');
	autoResize({ dataGrid: '#grid', callback: Initlist });
	$('body').layout();
});

function createColumnMenu() {
    var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
    var fields = grid.datagrid('getColumnFields');
    for (var i = 0; i < fields.length; i++) {
        $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
    }
    tmenu.menu({
        onClick: function (item) {
            if (item.iconCls == 'icon-ok') {
                grid.datagrid('hideColumn', item.text);
                tmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-empty'
                });
            } else {
                grid.datagrid('showColumn', item.text);
                tmenu.menu('setIcon', {
                    target: item.target,
                    iconCls: 'icon-ok'
                });
            }
        }
    });
}


var grid;
var searchWin;
var searchForm;
var AuthorizeWin;
var AuthorizeForm;

function getSelectedArr() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].ID);
    }
    return ids;
}
function getSelectedID() {
    var ids = getSelectedArr();
    return ids.join(',');
}
function arr2str(arr) {
    return arr.join(',');
}
function show(mytitle)
{
    $("#Dlg-Edit").dialog({
        title: mytitle
    });
}
function edit() {
    var rows = grid.datagrid('getSelections');
    var num = rows.length;
    if (num == 0) {
        Msgslide('请选择一条记录进行操作!'); //$.messager.alert('提示', '请选择一条记录进行操作!', 'info');
        return;
    }
    else if (num > 1) {
        Msgfade('您选择了多条记录,只能选择一条记录进行修改!'); //$.messager.alert('提示', '您选择了多条记录,只能选择一条记录进行修改!', 'info');
        return;
    }
    else {
        row =rows[0];
        $("#TxtName").val(rows [0].ExpertName);
        $('#SelectGender').combobox('setText',rows [0].Gender);
        $("#SelectZhiCheng").combobox('setText',rows [0].Title);
        $("#SelectZhiCheng").combobox('setValue',rows [0].TitleID);
        $("#TxtHospital").val(rows [0].BelongHospital);
        $("#TxtJiNeng").val(rows [0].Professionalskills);
        $("#SelectDepartment").combobox('setText',rows [0].BelongDepartment);
        $("#TxtSchool").val(rows [0].GraduateSchool);
        $("#TxtIntroduce").val(rows [0].ExpertIntroduction);
        show("修改专家信息");
        dlg_Edit.dialog('open');
        dlg_Edit_form.form('load', 'Handler/AjaxHandle.ashx' );
        dlg_Edit_form.url = 'Handler/AjaxHandle.ashx';
    }
}




function del() {
    var arr = getSelectedArr();
    if (arr.length > 0) {
        $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
            if (data) {
                var p = { AssemblyName: "OA.Bll.Authority", ClassName: "OA.Bll.Authority.UserInfoPresenter", Method: "Delete", Options: {} };
                p.Options ["ID"]=arr2str(arr);
                $.ajax({
                    url: 'Handler/AjaxHandle.ashx',
                    type: 'post',
                    dataType: 'json',
                    data:$.toJSON(p),
                    error: function (r) {
                        $.messager.alert('错误', r.msg, 'error');
                    },
                    success: function (r) {
                        eval('data=' + data);
                        if (r.flag) {
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            $.messager.alert('错误', r.msg, 'error');
                        }
                    }
                });
            }
        });
    } else {
        Msgshow('请先选择要删除的记录。');
    }
}

function Msgshow(msg) {
	$.messager.alert('提示', msg, 'info');
}

function Msgslide(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'slide'
    });
}
function Msgfade(msg) {
    $.messager.show({
        title: '提示',
        msg: msg,
        timeout: 3000,
        showType: 'fade'
    });
}
function OpensearchWin() {
    searchWin.window('open');
    searchForm.form('clear');
}

function SearchOK() {
    var sTitle = $("#txtname").val();
    var p = { AssemblyName: "OA.Bll.Authority", ClassName: "OA.Bll.Authority.UserInfoPresenter", Method: "GetUserRoleGroupList", Options: {} };
    p.Options["Username"] = sTitle;
    p.Options["GroupID"]=$('#SelectGroupList').combobox('getValue');
	grid.datagrid('options').pageNumber = 1;
    searchWin.window('close');
    grid.datagrid({ 
        methord: 'post',
        url: 'Handler/AjaxHandle.ashx', 
        queryParams:{ "json": $.toJSON(p)}
    });
	grid.datagrid("reload");
}
function closeSearchWindow() {
    searchWin.window('close');
}



function del() {
    var arr = getSelectedArr();
    if (arr.length > 0) {
        $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
            if (data) {
                var p = { AssemblyName: "OA.Bll.Authority", ClassName: "OA.Bll.Authority.UserInfoPresenter", Method: "Delete", Options: {} };
                p.Options ["ID"]=arr2str(arr);
                $.ajax({
                    url: 'Handler/AjaxHandle.ashx' ,
                    type: 'post',
                    dataType: 'json',
                    data:$.toJSON(p),
                    success: function (r) {
                        if (r.flag) {
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            $.messager.alert('错误',r .msg , 'error');
                        }
                    },
                 error: function (r) {
                    $.messager.alert("信息提示", r.msg, "error");
                  }
                });
            }
        });
    } else {
        Msgshow('请先选择要删除的记录。');
    }
}


//var ID;
//function OpenAuthorizeWin(id) {
//    ID=id;
//    AuthorizeWin.window('open');
//    AuthorizeForm.form('clear');
//}


function Initlist(size) {
    var p = { AssemblyName: "OA.Bll.Authority", ClassName: "OA.Bll.Authority.UserInfoPresenter", Method: "GetUserRoleGroupList", Options: {} };
    grid = $('#grid').datagrid({
        iconCls: 'icon-save',
        methord: 'post',
        url: '../../Handler/AjaxHandle.ashx',
        sortName: 'ID',
        queryParams: { "json": $.toJSON(p) },
        sortOrder: 'desc',
        idField: 'ID',
        width: size.width,
        height: size.height,
        pageSize: Math.round(size.height / 25),
        pageList: [Math.round(size.height / 25)],
        columns: [[
			{ field: 'ID', title: '编号', width: 150, hidden: true },
			{ field: 'UserName', title: '用户名', width: 150 },
			{ field: 'RoleName', title: '用户角色', width: 150, sortable: true },
			{ field: 'GroupName', title: '用户组', width: 150, sortable: true },
			{ field: 'Status', title: '状态', width: 150, sortable: true },
			{ field: 'CreateTime', title: '创建时间', width: 150, sortable: true },
			{ field: 'LastLoginTime', title: '最后一次登录时间', width: 150, sortable: true },
			{
			    field: 'op',
			    title: '授权',
			    width: 120,
			    formatter: function(value, rowData, index) {
			        return "<a href='UserGrant.aspx?uid=" + rowData.Id+ "&name=" + rowData.UserName + "'>授权</a>";
			    }
			}
		]],
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [
        //        	{
        //            text: '删除',
        //            iconCls: 'icon-remove',
        //            handler: del
        //        }, '-', 
			{
			text: '查找',
			iconCls: 'icon-search',
			handler: OpensearchWin
}],
        onHeaderContextMenu: function(e, field) {
            e.preventDefault();
            if (!$('#tmenu').length) {
                createColumnMenu();
            }
            $('#tmenu').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });
}